/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.repair.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.repair.entity.*;
import org.springblade.modules.repair.service.*;
import org.springblade.modules.repair.vo.MaintainPredicamentDetailResultVo;
import org.springblade.modules.repair.vo.plan.MaintainPredicamentDetailVo;
import org.springblade.modules.repair.vo.plan.RecentlyPlanVo;
import org.springblade.modules.system.service.IUserService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * 困人详情 控制器
 *
 * @author zcc
 * @since 2021-05-24
 */
@RestController
@AllArgsConstructor
@RequestMapping("repair/maintain/predicament/detail")
@Api(value = "困人详情", tags = "困人详情管理")
public class MaintainPredicamentDetailController extends BladeController {

	private final IMaintainPredicamentDetailService maintainPredicamentDetailService;

	private final IMaintainPlanContentService maintainPlanContentService;

	private final IMaintainMalfunctionLogService maintainMalfunctionLogService;

	private final IMaintainPlanContentUserService maintainPlanContentUserService;

	private final IUserService userService;

	private final ElevatorDetailService elevatorDetailService;

	private final IElevatorService elevatorService;

	private final IMaintainMalfunctionHistoryService maintainMalfunctionHistoryService;


	/**
	 * 已解救
	 */
	private final static Integer STATUS = 2;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入maintainPredicamentDetail")
	public R<MaintainPredicamentDetailResultVo> detail(@RequestParam("planId") Long planId) {

		MaintainPlanContent maintainPlanContent = maintainPlanContentService.getById(planId);
		MaintainPredicamentDetailResultVo maintainPredicamentDetailResultVo = new MaintainPredicamentDetailResultVo();
		MaintainPredicamentDetail maintainPredicamentDetail = maintainPredicamentDetailService.getOne(Wrappers.<MaintainPredicamentDetail>lambdaQuery().eq(MaintainPredicamentDetail::getPlanId, planId));
		BeanUtil.copyProperties(maintainPredicamentDetail, maintainPredicamentDetailResultVo);
		maintainPredicamentDetailResultVo.setElevatorId(maintainPlanContent.getElevatorId());
		maintainPredicamentDetailResultVo.setPlanStatus(maintainPlanContent.getStatus());
		maintainPredicamentDetailResultVo.setBuildPhone(userService.getById(maintainPlanContent.getCreateUser()).getPhone());
		return R.data(maintainPredicamentDetailResultVo);
	}

	/**
	 * 详情
	 */
	@GetMapping("/v2/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入maintainPredicamentDetail")
	public R<MaintainPredicamentDetailVo> detailV2(@RequestParam("planId") Long planId) {

		MaintainPredicamentDetailVo maintainPredicamentDetailVo = new MaintainPredicamentDetailVo();
		MaintainPlanContent maintainPlanContent = maintainPlanContentService.getById(planId);
		MaintainPlanContentUser planContentUser = maintainPlanContentUserService.getOne(Wrappers.<MaintainPlanContentUser>lambdaQuery().eq(MaintainPlanContentUser::getPlanId, planId).eq(MaintainPlanContentUser::getUserId, AuthUtil.getUserId()));
		maintainPlanContent.setUpdateTime(planContentUser.getCreateTime());
		maintainPredicamentDetailVo.setPlanStatus(maintainPlanContent.getStatus());
		MaintainPredicamentDetail maintainPredicamentDetail = maintainPredicamentDetailService.getOne(Wrappers.<MaintainPredicamentDetail>lambdaQuery().eq(MaintainPredicamentDetail::getPlanId, planId));
		maintainPredicamentDetail.setElevatorIsStop(elevatorService.getById(maintainPlanContent.getId()).getIsStop());
		maintainPredicamentDetailVo.setMaintainPredicamentDetail(maintainPredicamentDetail);
		maintainPredicamentDetailVo.setElevatorDetail(elevatorDetailService.getOne(Wrappers.<ElevatorDetail>lambdaQuery().eq(ElevatorDetail::getElevatorId, maintainPlanContent.getElevatorId())));

		RecentlyPlanVo recentlyPlanVo = maintainPredicamentDetailService.getRecentlyPlanDetail(maintainPlanContent.getElevatorId());
		maintainPredicamentDetailVo.setRecentlyPlanVo(recentlyPlanVo);

		maintainPredicamentDetailVo.setBuildPhone(userService.getById(maintainPlanContent.getCreateUser()).getPhone());

		List<MaintainMalfunctionHistory> list = maintainMalfunctionHistoryService.list(Wrappers.<MaintainMalfunctionHistory>lambdaQuery().eq(MaintainMalfunctionHistory::getPlanId, planId).orderByDesc(MaintainMalfunctionHistory::getCreateTime));
		maintainPredicamentDetailVo.setList(list);

		return R.data(maintainPredicamentDetailVo);
	}

	/**
	 * 修改 困人详情
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入maintainPredicamentDetail")
	public R update(@Valid @RequestBody MaintainPredicamentDetail maintainPredicamentDetail) {

		if (maintainPredicamentDetail.getStatus().equals(STATUS)) {
			MaintainPredicamentDetail predicamentDetail = maintainPredicamentDetailService.getById(maintainPredicamentDetail.getId());
			MaintainPlanContent maintainPlanContent = maintainPlanContentService.getById(predicamentDetail.getPlanId());
			Date createTime = maintainPlanContent.getCreateTime();
			MaintainMalfunctionLog maintainMalfunctionLog = new MaintainMalfunctionLog();
			maintainMalfunctionLog.setTypeName("被困人已解救，用时" + DateUtil.between(createTime, new Date(), DateUnit.MINUTE) + "分钟");
			maintainMalfunctionLog.setPlanId(predicamentDetail.getPlanId());
			maintainMalfunctionLogService.save(maintainMalfunctionLog);
		}
		return R.status(maintainPredicamentDetailService.updateById(maintainPredicamentDetail));
	}

}
